<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div {
            margin: 200px;
        }
        
        span {
            display: inline-block;
            width: 40px;
            height: 40px;
            background-color: #333;
            font-size: 20px;
            color: #fff;
            text-align: center;
            line-height: 40px;
        }
    </style>
</head>
<body>
    <div>
        <span class="hour">1</span>
        <span class="minute">2</span>
        <span class="second">3</span>
    </div>
    <button class="begin">开始</button>
    <button class="end">结束</button>
    <script>
        var H = document.querySelector('.hour');
        var M  = document.querySelector('.minute');
        var S = document.querySelector('.second');
        var b = document.querySelector('.begin');
        var e = document.querySelector('.end');
        function countDown(time) {
            var nowTime = +new Date();
            var inputTime = +new Date(time);//返回的是用户输入时间的毫秒数
            var times = inputTime / 1000 - nowTime / 1000;
            d = parseInt(times / 60 / 60 / 24);//天
            d = d < 10 ? '0' + d : d;
            h = parseInt(times / 60 / 60);//时
            h = h < 10 ? '0' + h : h;
            m = parseInt(times / 60 % 60);//分
            m = m < 10 ? '0' + m : m;
            s = parseInt(times % 60);//秒
            s = s < 10 ? '0' + s : s;
            H.innerHTML = h;
            M.innerHTML = m;
            S.innerHTML = s;
            return d + '天' + h + '时' + m + '分' + s + '秒';
        }
        countDown('2023-5-1 1:00:00')
        var timer = null;
        b.addEventListener('click',function(){
            timer = setInterval("countDown('2023-5-1 1:00:00')")
        })
        e.addEventListener('click',function(){
            //清除定时器clearInterval
            clearInterval(timer);
        })
    </script>
</body>
</html>